package com.magenta.maxoptra.gis.utils;

import java.io.*;

/**
 * Comment.
 * User: veretennikov
 * Date: 26.07.12
 * Time: 17:51
 */
public class StreamUtils {
    public static <T> T deserialize(byte[] buffer) throws IOException, ClassNotFoundException {
        ObjectInputStream objectIn = null;
        if (buffer != null)
            objectIn = new ObjectInputStream(new ByteArrayInputStream(buffer));

        T deserializedObject = (T) objectIn.readObject();
        return deserializedObject;
    }

    public static <T> byte[] serialize(T object) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            final ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
